<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微信朋友圈朋友圈排列</title>
    <!-- 微信朋友圈朋友圈 -->
    <style>
        .tl-photos {
            display: flex;
            width: 400px;
            flex-wrap: wrap;
        }

        /* 只有一张照片时 */
        .tl-photos>img:only-child {
            width: 100%;
        }

        /* 有2张照片时 */
        .tl-photos>img:first-child:nth-last-child(2),
        .tl-photos>img:nth-child(2):nth-last-child(1) {
            width: 198px;
        }

        .tl-photos>img:nth-child(2):nth-last-child(1) {
            margin-left: 3px;
        }

        /* 有3张照片时 */
        .tl-photos>img:first-child:nth-last-child(3),
        .tl-photos>img:nth-child(2):nth-last-child(2),
        .tl-photos>img:nth-child(3):nth-last-child(1) {
            width: 130px;
        }

        .tl-photos>img:nth-child(2):nth-last-child(2),
        .tl-photos>img:nth-child(3):nth-last-child(1) {
            margin-left: 3.33px;
        }

        /* 有4张照片时 */
        .tl-photos>img:first-child:nth-last-child(4),
        .tl-photos>img:first-child:nth-last-child(4)~img {
            width: 198px;
        }

        .tl-photos>img:nth-child(2):nth-last-child(3),
        .tl-photos>img:nth-child(4):nth-last-child(1) {
            margin-left: 3px;
        }

        .tl-photos>img:nth-child(2):nth-last-child(3)~img {
            margin-top: 3px;
        }

        /* 有5张以照片 */
        .tl-photos>img:first-child:nth-last-child(n+5),
        .tl-photos>img:first-child:nth-last-child(n+5)~img {
            width: 130px;
        }

        .tl-photos>img:nth-child(3n-1),
        .tl-photos>img:nth-child(3n) {
            margin-left: 3px;
        }

        /* 解决上面的 margin-left 导致只有 4张照片时候的特殊情况 */
        .tl-photos>img:nth-child(3):nth-last-child(2) {
            margin-left: 0px;
        }

        .tl-photos>img:nth-child(3)~img {
            margin-top: 3px;
        }
    </style>
</head>

<body>
    <div class="tl-photos">
        <img src="./img/1.jpg" alt="">
        <img src="./img/2.jpg" alt="">
        <img src="./img/3.jpg" alt="">
        <img src="./img/4.jpg" alt="">
        <img src="./img/5.jpg" alt="">
        <img src="./img/6.jpg" alt="">
        <img src="./img/7.jpg" alt="">
        <img src="./img/8.jpg" alt="">
        <img src="./img/9.jpg" alt="">
    </div>
</body>

</html>